package com.example.ccj.b.c.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.ccj.R;


/**
 * 拨码样式的checkbox
 */
public class BoMaKaiGuanControl extends RelativeLayout {
    private TextView bottom;
    private TextView boMa;
    private String openText = "开";
    private String closeText = "关";
    private boolean isSelect = false;

    public BoMaKaiGuanControl(Context context) {
        super(context);
    }

    public BoMaKaiGuanControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(getContext()).inflate(R.layout.control_check_box_switch,this);
        bottom = findViewById(R.id.control_check_box_switch_back);
        boMa = findViewById(R.id.control_check_box_switch_boma);
        bottom.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                isSelect = !isSelect;
                modifyState(isSelect);
            }
        });

        boMa.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                isSelect = !isSelect;
                modifyState(isSelect);
            }
        });
    }

    /**
     * 设置点击回调
     * @param boMaOnClickListener
     */
    public void setBoMaOnClickListener(BoMaOnClickListener boMaOnClickListener){
        if (boMaOnClickListener == null) return;

        bottom.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                isSelect = !isSelect;
                modifyState(isSelect);
                boMaOnClickListener.onClick(isSelect);
            }
        });

        boMa.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                isSelect = !isSelect;
                modifyState(isSelect);
                boMaOnClickListener.onClick(isSelect);
            }
        });
    }

    /**
     * 切换拨码样式逻辑
     * @param isSelect
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    public void modifyState(boolean isSelect){
        if (isSelect) {
            boMa.setText(openText);
            bottom.setBackground(getResources().getDrawable(R.drawable.control_bo_ma_selected_background));
            boMa.setBackground(getResources().getDrawable(R.drawable.control_bo_ma_selected));
            LayoutParams layoutParams = (LayoutParams)boMa.getLayoutParams();
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,R.id.control_check_box_switch_back);
            boMa.setLayoutParams(layoutParams);
        } else {
            boMa.setText(closeText);
            bottom.setBackground(getResources().getDrawable(R.drawable.control_bo_ma_no_select));
            boMa.setBackground(getResources().getDrawable(R.drawable.control_bo_ma_yuan_no_select));
            LayoutParams layoutParams = (LayoutParams)boMa.getLayoutParams();
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,R.id.control_check_box_switch_back);
            boMa.setLayoutParams(layoutParams);
        }
        this.isSelect = isSelect;
    }

    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
        modifyState(select);
    }

    public TextView getBoMa() {
        return boMa;
    }

    public void setBoMa(TextView boMa) {
        this.boMa = boMa;
    }

    public String getOpenText() {
        return openText;
    }

    public void setOpenText(String openText) {
        this.openText = openText;
    }

    public String getCloseText() {
        return closeText;
    }

    public void setCloseText(String closeText) {
        this.closeText = closeText;
    }
}
